package com.example.shop.utils;

import cn.hutool.core.date.DateUtil;
import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.example.shop.entity.User;
import com.example.shop.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;
import com.auth0.jwt.exceptions.JWTVerificationException;
import java.util.Date;

@Component
public class JwtUtil {
    private static UserService staticuserService;
    //生成token  生成token  生成token
    public static String createToken(String data,String sign){
        return JWT.create().withAudience(data+"-"+"admin")//将data数据放入载荷中,id-admin
                .withExpiresAt(DateUtil.offsetDay(new Date(),1))//设置过期时间,一天.目前时间向后偏移一天
                .sign(Algorithm.HMAC256(sign));//将sign作为密钥进行加密,使用的算法是HMAC256
    }

//    public static boolean checkToken(String token){
//        String username = JWT.decode(token).getAudience().get(0);
//        User user = (User) staticuserService.findByUsername(username);
//        if(user==null)
//            System.out.println("checkToken时,user不存在");
//        try {
//            JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256(user.getPassword())).build();
//            jwtVerifier.verify(token);
//        }catch (JWTVerificationException e){
//            System.out.println("JWT验证失败");
//            return false;
//        }
//        return true;
//    }
}
